package com.moleq.service;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.moleq.db.DBError;
import com.moleq.db.DatabaseHelper;
import com.moleq.db.DatabaseSchema;
import com.moleq.device.PaxPinPadSetting;
import com.moleq.models.Pinpad;

public class PinPadService {
	private DatabaseHelper DBHelper;
	private String sql; 
	
	public PinPadService() {
		
	}
	
	public PinPadService(Context shareContext) {
		this.DBHelper = new DatabaseHelper(shareContext);
	}

	public PaxPinPadSetting getPinPadSetting() {
		PaxPinPadSetting obj = null;
		
		SQLiteDatabase db = DBHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery("SELECT * FROM t_pinpad_setting where pp_using ='Y'", null);
		while (cursor.moveToNext()) {
			int no = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.PinPadSetting.PP_NO));
			int port = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.PinPadSetting.PORT));
			String device = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PinPadSetting.DEVICE_NAME));
			int timeOut = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.PinPadSetting.TIME_OUT));
			String destip = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PinPadSetting.DEST_IP));
			String using = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PinPadSetting.USING));
			String signature_fl = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PinPadSetting.SIGNATURE_FL));
			String destport = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PinPadSetting.PP_DEST_PORT));
			String con_type = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PinPadSetting.PP_CON_TYPE));
			obj = new PaxPinPadSetting(no, port, device, timeOut, destip, using,signature_fl,con_type, destport);
		}
		cursor.close();
		return obj;
	}
	
	public List<Pinpad> getAllPinpad(){
		sql = "select * from t_pinpad_setting";
		SQLiteDatabase db = DBHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery(sql, null);
		List<Pinpad> Source = new ArrayList<Pinpad>();
		if (cursor == null) {
//			ShowMessage(DBError.error,mMessageDialog.MESSAGE_BOX_OK,mMessageDialog.ICON_ERROR);
			DBError.error = "";
			return null;
		}
		while (cursor.moveToNext()) {
			Pinpad pp = new Pinpad(
					cursor.getInt(0),
					cursor.getInt(1),
					cursor.getString(2),
					cursor.getInt(3),
					cursor.getString(4),
					cursor.getString(5),
					cursor.getString(6),
					cursor.getString(7),
					cursor.getString(8),
					cursor.getInt(9),
					cursor.getString(10));
			Source.add(pp);
		}
		cursor.close();
		return Source;
	}
	
	public String insertPinpad(Pinpad pinpad){
		String errorString = "";
		String sql = "Insert into t_pinpad_setting values(";
		if (pinpad.getDestport() == -1) {
			sql += pinpad.getNo()+ "," + pinpad.getPort() + ",'" + pinpad.getDeviceName() + "',"+
					pinpad.getTimeout() + ",'" + pinpad.getDestip() + "','" + pinpad.getUsing() + "'," +
					"strftime('%Y%m%d%H%M%S','now'),strftime('%Y%m%d%H%M%S','now')," + pinpad.getSignature_fl() + "," +
					null + ",'"+ pinpad.getCon_type() +"')";
		}else{
			sql += pinpad.getNo()+ "," + pinpad.getPort() + ",'" + pinpad.getDeviceName() + "',"+
					pinpad.getTimeout() + ",'" + pinpad.getDestip() + "','" + pinpad.getUsing() + "'," +
					"strftime('%Y%m%d%H%M%S','now'),strftime('%Y%m%d%H%M%S','now')," + pinpad.getSignature_fl() + "," +
					pinpad.getDestport() + ",'"+ pinpad.getCon_type() +"')";
		}
		SQLiteDatabase db = DBHelper.getReadableDatabase();
		try{
			db.execSQL(sql);
		}catch (Exception e) {
			// TODO: handle exception
			errorString = e.getMessage();
		}
		return errorString;
	}
	
	public String updatePinpad(Pinpad pinpad){
		String errorString = "";
		sql = "update t_pinpad_setting set ";
		sql += "pp_port = " + pinpad.getPort() + ",";
		if (pinpad.getDeviceName() != null && !pinpad.getDeviceName().equals("")) {
			sql += "pp_device_name = '"+ pinpad.getDeviceName() +"',";
		}else {
			sql += "pp_device_name = " + null + ",";
		}
		if (pinpad.getTimeout() >= 0) {
			sql += "pp_timeout = " + pinpad.getTimeout() +",";
		}else {
			sql += "pp_timeout = " + null + ",";
		}
		if (pinpad.getDestip() != null && !pinpad.getDestip().equals("")) {
			sql += "pp_destip = '"+ pinpad.getDestip() +"',";
		}else {
			sql += "pp_destip = " + null + ",";
		}
		if(pinpad.getUsing() != null && !pinpad.getUsing().equals("")){
			sql += "pp_using = '" + pinpad.getUsing() + "',";
		}else{
			sql += "pp_using = " + null + ",";
		}
		sql += "update_datetime = strftime('%Y%m%d%H%M%S','now'),";
		sql += "signature_fl = " + pinpad.getSignature_fl()+ ",";
		if (pinpad.getDestport() == -1) {
			sql += "pp_destport = " + null + ",";
		}else{
			sql += "pp_destport = " + pinpad.getDestport()+ ",";
		}
		sql += "pp_con_type = '" + pinpad.getCon_type() + "'";
		sql += " where pp_no = " + pinpad.getNo();
		SQLiteDatabase db = DBHelper.getReadableDatabase();
		try{
			db.execSQL(sql);
		}catch (Exception e) {
			// TODO: handle exception
			errorString = e.getMessage();
		}
		return errorString;
	}
	
	public String deletePinpad(int no){
		String errorString = "";
		sql = "Delete from t_pinpad_setting where pp_no = " + no;
		SQLiteDatabase db = DBHelper.getReadableDatabase();
		try{
			db.execSQL(sql);
		}catch (Exception e) {
			// TODO: handle exception
			errorString = e.getMessage();
		}
		return errorString;
	}
	
	public Pinpad FindPinpadByNo(int no){
		String sql = "select * from t_pinpad_setting where pp_no = " + no;
		SQLiteDatabase db = DBHelper.getReadableDatabase();
		Pinpad pinpad = null;
		Cursor cursor = db.rawQuery(sql, null);
		if(cursor == null){
			return pinpad;
		}
		if(cursor.moveToNext()){
			pinpad = new Pinpad(
					cursor.getInt(0),
					cursor.getInt(1),
					cursor.getString(2),
					cursor.getInt(3),
					cursor.getString(4),
					cursor.getString(5),
					cursor.getString(6),
					cursor.getString(7),
					cursor.getString(8),
					cursor.getInt(9),
					cursor.getString(10));
		}
		cursor.close();
		return pinpad;
	}
	
	public int getMaxNo(){
		
		sql = "select Max(pp_no) from t_pinpad_setting";
		SQLiteDatabase db = DBHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery(sql, null);
		int no = 0;
		if(cursor == null){
//			ShowMessage(DBError.error,mMessageDialog.MESSAGE_BOX_OK,mMessageDialog.ICON_ERROR);
			DBError.error = "";
			return no;
		}
		while (cursor.moveToNext()) {
			no = cursor.getInt(0);
		}
		cursor.close();
		return no;
	}

}
